package com.yonyou.demo;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.project.ProjectManager;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiManager;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.psi.search.FileTypeIndex;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.search.ProjectScope;
import com.yonyou.common.dialog.ReplaceDialogWrapper;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.regex.Pattern;

import java.util.Collection;

public class CtmDemo extends AnAction {

    @Override
    public void actionPerformed(AnActionEvent e) {

        Project project = e.getData(PlatformDataKeys.PROJECT);
        List<VirtualFile> vFiles=getAllJavaFilePaths();
        ReplaceDialogWrapper dialog = new ReplaceDialogWrapper(project,vFiles);
        dialog.setResizable(true); //是否允许用户通过拖拽的方式扩大或缩小你的表单框，我这里定义为true，表示允许
        dialog.show();


    }

    public static List<VirtualFile> getAllJavaFilePaths() {
        List<VirtualFile> javaFilePaths = new ArrayList<>();

        // 获取所有打开的项目
        Project[] projects = ProjectManager.getInstance().getOpenProjects();
        for (Project project : projects) {
            // 获取项目范围
            GlobalSearchScope scope = ProjectScope.getProjectScope(project);

            // 使用 FileTypeIndex 来搜索 Java 文件
            Collection<VirtualFile> virtualFiles = FileTypeIndex.getFiles(com.intellij.openapi.fileTypes.StdFileTypes.JAVA, scope);

            // 将 VirtualFile 转换为文件路径
            for (VirtualFile virtualFile : virtualFiles) {
                javaFilePaths.add(virtualFile);
            }
        }

        return javaFilePaths;
    }
    public static boolean replaceTextInFile(Project project, VirtualFile virtualFile, String oldText, String newText) {
        AtomicBoolean flags = new AtomicBoolean(false);
        WriteCommandAction.runWriteCommandAction(project, () -> {
            try {
                // 获取文件对应的 PsiFile，这样可以确保文档是同步的
                PsiFile psiFile = PsiManager.getInstance(project).findFile(virtualFile);
                if (psiFile != null) {
                    Document document = FileDocumentManager.getInstance().getDocument(virtualFile);
                    if (document != null) {
                        String text = document.getText();
                        String newTextContent = text.replaceAll(Pattern.quote(oldText), newText);
                        document.setText(newTextContent);
                        flags.set(true);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace(); // 适当的错误处理
            }
        });
        return flags.get();
    }

}
